using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;
namespace SubSonicResearch
{
	/// <summary>
	/// Strongly-typed collection for the Person class.
	/// </summary>
    [Serializable]
	public partial class PersonCollection : ActiveList<Person, PersonCollection>
	{	   
		public PersonCollection() {}
        
        /// <summary>
		/// Filters an existing collection based on the set criteria. This is an in-memory filter
		/// Thanks to developingchris for this!
        /// </summary>
        /// <returns>PersonCollection</returns>
		public PersonCollection Filter()
        {
            for (int i = this.Count - 1; i > -1; i--)
            {
                Person o = this[i];
                foreach (SubSonic.Where w in this.wheres)
                {
                    bool remove = false;
                    System.Reflection.PropertyInfo pi = o.GetType().GetProperty(w.ColumnName);
                    if (pi.CanRead)
                    {
                        object val = pi.GetValue(o, null);
                        switch (w.Comparison)
                        {
                            case SubSonic.Comparison.Equals:
                                if (!val.Equals(w.ParameterValue))
                                {
                                    remove = true;
                                }
                                break;
                        }
                    }
                    if (remove)
                    {
                        this.Remove(o);
                        break;
                    }
                }
            }
            return this;
        }
		
		
	}
	/// <summary>
	/// This is an ActiveRecord class which wraps the People table.
	/// </summary>
	[Serializable]
	public partial class Person : ActiveRecord<Person>, IActiveRecord
	{
		#region .ctors and Default Settings
		
		public Person()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}
		
		private void InitSetDefaults() { SetDefaults(); }
		
		public Person(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}
        
		public Person(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}
		 
		public Person(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}
		
		protected static void SetSQLProps() { GetTableSchema(); }
		
		#endregion
		
		#region Schema and Query Accessor	
		public static Query CreateQuery() { return new Query(Schema); }
		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}
		}
		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("People", TableType.Table, DataService.GetInstance("ssDSN"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarPersonId = new TableSchema.TableColumn(schema);
				colvarPersonId.ColumnName = "PersonId";
				colvarPersonId.DataType = DbType.Int32;
				colvarPersonId.MaxLength = 0;
				colvarPersonId.AutoIncrement = true;
				colvarPersonId.IsNullable = false;
				colvarPersonId.IsPrimaryKey = true;
				colvarPersonId.IsForeignKey = false;
				colvarPersonId.IsReadOnly = false;
				colvarPersonId.DefaultSetting = @"";
				colvarPersonId.ForeignKeyTableName = "";
				schema.Columns.Add(colvarPersonId);
				
				TableSchema.TableColumn colvarFirstName = new TableSchema.TableColumn(schema);
				colvarFirstName.ColumnName = "FirstName";
				colvarFirstName.DataType = DbType.AnsiString;
				colvarFirstName.MaxLength = 50;
				colvarFirstName.AutoIncrement = false;
				colvarFirstName.IsNullable = false;
				colvarFirstName.IsPrimaryKey = false;
				colvarFirstName.IsForeignKey = false;
				colvarFirstName.IsReadOnly = false;
				colvarFirstName.DefaultSetting = @"";
				colvarFirstName.ForeignKeyTableName = "";
				schema.Columns.Add(colvarFirstName);
				
				TableSchema.TableColumn colvarLastName = new TableSchema.TableColumn(schema);
				colvarLastName.ColumnName = "LastName";
				colvarLastName.DataType = DbType.AnsiString;
				colvarLastName.MaxLength = 50;
				colvarLastName.AutoIncrement = false;
				colvarLastName.IsNullable = false;
				colvarLastName.IsPrimaryKey = false;
				colvarLastName.IsForeignKey = false;
				colvarLastName.IsReadOnly = false;
				colvarLastName.DefaultSetting = @"";
				colvarLastName.ForeignKeyTableName = "";
				schema.Columns.Add(colvarLastName);
				
				TableSchema.TableColumn colvarEmail = new TableSchema.TableColumn(schema);
				colvarEmail.ColumnName = "Email";
				colvarEmail.DataType = DbType.AnsiString;
				colvarEmail.MaxLength = 128;
				colvarEmail.AutoIncrement = false;
				colvarEmail.IsNullable = false;
				colvarEmail.IsPrimaryKey = false;
				colvarEmail.IsForeignKey = false;
				colvarEmail.IsReadOnly = false;
				colvarEmail.DefaultSetting = @"";
				colvarEmail.ForeignKeyTableName = "";
				schema.Columns.Add(colvarEmail);
				
				TableSchema.TableColumn colvarCompanyId = new TableSchema.TableColumn(schema);
				colvarCompanyId.ColumnName = "CompanyId";
				colvarCompanyId.DataType = DbType.Int32;
				colvarCompanyId.MaxLength = 0;
				colvarCompanyId.AutoIncrement = false;
				colvarCompanyId.IsNullable = true;
				colvarCompanyId.IsPrimaryKey = false;
				colvarCompanyId.IsForeignKey = true;
				colvarCompanyId.IsReadOnly = false;
				colvarCompanyId.DefaultSetting = @"";
				
					colvarCompanyId.ForeignKeyTableName = "Companies";
				schema.Columns.Add(colvarCompanyId);
				
				TableSchema.TableColumn colvarRoleId = new TableSchema.TableColumn(schema);
				colvarRoleId.ColumnName = "RoleId";
				colvarRoleId.DataType = DbType.Int32;
				colvarRoleId.MaxLength = 0;
				colvarRoleId.AutoIncrement = false;
				colvarRoleId.IsNullable = true;
				colvarRoleId.IsPrimaryKey = false;
				colvarRoleId.IsForeignKey = true;
				colvarRoleId.IsReadOnly = false;
				colvarRoleId.DefaultSetting = @"";
				
					colvarRoleId.ForeignKeyTableName = "Roles";
				schema.Columns.Add(colvarRoleId);
				
				TableSchema.TableColumn colvarManagerPersonId = new TableSchema.TableColumn(schema);
				colvarManagerPersonId.ColumnName = "ManagerPersonId";
				colvarManagerPersonId.DataType = DbType.Int32;
				colvarManagerPersonId.MaxLength = 0;
				colvarManagerPersonId.AutoIncrement = false;
				colvarManagerPersonId.IsNullable = true;
				colvarManagerPersonId.IsPrimaryKey = false;
				colvarManagerPersonId.IsForeignKey = true;
				colvarManagerPersonId.IsReadOnly = false;
				colvarManagerPersonId.DefaultSetting = @"";
				
					colvarManagerPersonId.ForeignKeyTableName = "People";
				schema.Columns.Add(colvarManagerPersonId);
				
				TableSchema.TableColumn colvarUserType = new TableSchema.TableColumn(schema);
				colvarUserType.ColumnName = "UserType";
				colvarUserType.DataType = DbType.AnsiStringFixedLength;
				colvarUserType.MaxLength = 2;
				colvarUserType.AutoIncrement = false;
				colvarUserType.IsNullable = false;
				colvarUserType.IsPrimaryKey = false;
				colvarUserType.IsForeignKey = false;
				colvarUserType.IsReadOnly = false;
				colvarUserType.DefaultSetting = @"";
				colvarUserType.ForeignKeyTableName = "";
				schema.Columns.Add(colvarUserType);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["ssDSN"].AddSchema("People",schema);
			}
		}
		#endregion
		
		#region Props
		  
		[XmlAttribute("PersonId")]
		[Bindable(true)]
		public int PersonId 
		{
			get { return GetColumnValue<int>(Columns.PersonId); }
			set { SetColumnValue(Columns.PersonId, value); }
		}
		  
		[XmlAttribute("FirstName")]
		[Bindable(true)]
		public string FirstName 
		{
			get { return GetColumnValue<string>(Columns.FirstName); }
			set { SetColumnValue(Columns.FirstName, value); }
		}
		  
		[XmlAttribute("LastName")]
		[Bindable(true)]
		public string LastName 
		{
			get { return GetColumnValue<string>(Columns.LastName); }
			set { SetColumnValue(Columns.LastName, value); }
		}
		  
		[XmlAttribute("Email")]
		[Bindable(true)]
		public string Email 
		{
			get { return GetColumnValue<string>(Columns.Email); }
			set { SetColumnValue(Columns.Email, value); }
		}
		  
		[XmlAttribute("CompanyId")]
		[Bindable(true)]
		public int? CompanyId 
		{
			get { return GetColumnValue<int?>(Columns.CompanyId); }
			set { SetColumnValue(Columns.CompanyId, value); }
		}
		  
		[XmlAttribute("RoleId")]
		[Bindable(true)]
		public int? RoleId 
		{
			get { return GetColumnValue<int?>(Columns.RoleId); }
			set { SetColumnValue(Columns.RoleId, value); }
		}
		  
		[XmlAttribute("ManagerPersonId")]
		[Bindable(true)]
		public int? ManagerPersonId 
		{
			get { return GetColumnValue<int?>(Columns.ManagerPersonId); }
			set { SetColumnValue(Columns.ManagerPersonId, value); }
		}
		  
		[XmlAttribute("UserType")]
		[Bindable(true)]
		public string UserType 
		{
			get { return GetColumnValue<string>(Columns.UserType); }
			set { SetColumnValue(Columns.UserType, value); }
		}
		
		#endregion
		
		
		#region PrimaryKey Methods		
		
        protected override void SetPrimaryKey(object oValue)
        {
            base.SetPrimaryKey(oValue);
            
            SetPKValues();
        }
        
		
		public SubSonicResearch.AddressCollection Addresses()
		{
			return new SubSonicResearch.AddressCollection().Where(Address.Columns.PersonId, PersonId).Load();
		}
		public SubSonicResearch.MessageCollection Messages()
		{
			return new SubSonicResearch.MessageCollection().Where(Message.Columns.SenderUserId, PersonId).Load();
		}
		public SubSonicResearch.PersonCollection ChildPeople()
		{
			return new SubSonicResearch.PersonCollection().Where(Person.Columns.ManagerPersonId, PersonId).Load();
		}
		public SubSonicResearch.PortalPeopleXrefLCollection PortalPeopleXrefLs()
		{
			return new SubSonicResearch.PortalPeopleXrefLCollection().Where(PortalPeopleXrefL.Columns.PersonId, PersonId).Load();
		}
		public SubSonicResearch.PortalPeopleXrefCollection PortalPeopleXrefRecords()
		{
			return new SubSonicResearch.PortalPeopleXrefCollection().Where(PortalPeopleXref.Columns.PersonId, PersonId).Load();
		}
		public SubSonicResearch.PortalCollection Portals()
		{
			return new SubSonicResearch.PortalCollection().Where(Portal.Columns.AdminUserId, PersonId).Load();
		}
		public SubSonicResearch.RecipientCollection Recipients()
		{
			return new SubSonicResearch.RecipientCollection().Where(Recipient.Columns.UserId, PersonId).Load();
		}
		#endregion
		
			
		
		#region ForeignKey Properties
		
		/// <summary>
		/// Returns a Company ActiveRecord object related to this Person
		/// 
		/// </summary>
		public SubSonicResearch.Company Company
		{
			get { return SubSonicResearch.Company.FetchByID(this.CompanyId); }
			set { SetColumnValue("CompanyId", value.CompanyId); }
		}
		
		
		/// <summary>
		/// Returns a Role ActiveRecord object related to this Person
		/// 
		/// </summary>
		public SubSonicResearch.Role Role
		{
			get { return SubSonicResearch.Role.FetchByID(this.RoleId); }
			set { SetColumnValue("RoleId", value.RoleId); }
		}
		
		
		/// <summary>
		/// Returns a Person ActiveRecord object related to this Person
		/// 
		/// </summary>
		public SubSonicResearch.Person ParentPerson
		{
			get { return SubSonicResearch.Person.FetchByID(this.ManagerPersonId); }
			set { SetColumnValue("ManagerPersonId", value.PersonId); }
		}
		
		
		#endregion
		
		
		
		#region Many To Many Helpers
		
		 
		public SubSonicResearch.PortalCollection GetPortalCollection() { return Person.GetPortalCollection(this.PersonId); }
		public static SubSonicResearch.PortalCollection GetPortalCollection(int varPersonId)
		{
		    SubSonic.QueryCommand cmd = new SubSonic.QueryCommand("SELECT * FROM [dbo].[Portals] INNER JOIN [PortalPeopleXref] ON [Portals].[PortalId] = [PortalPeopleXref].[PortalId] WHERE [PortalPeopleXref].[PersonId] = @PersonId", Person.Schema.Provider.Name);
			cmd.AddParameter("@PersonId", varPersonId, DbType.Int32);
			IDataReader rdr = SubSonic.DataService.GetReader(cmd);
			PortalCollection coll = new PortalCollection();
			coll.LoadAndCloseReader(rdr);
			return coll;
		}
		
		public static void SavePortalMap(int varPersonId, PortalCollection items)
		{
			QueryCommandCollection coll = new SubSonic.QueryCommandCollection();
			//delete out the existing
			QueryCommand cmdDel = new QueryCommand("DELETE FROM [PortalPeopleXref] WHERE [PortalPeopleXref].[PersonId] = @PersonId", Person.Schema.Provider.Name);
			cmdDel.AddParameter("@PersonId", varPersonId, DbType.Int32);
			coll.Add(cmdDel);
			DataService.ExecuteTransaction(coll);
			foreach (Portal item in items)
			{
				PortalPeopleXref varPortalPeopleXref = new PortalPeopleXref();
				varPortalPeopleXref.SetColumnValue("PersonId", varPersonId);
				varPortalPeopleXref.SetColumnValue("PortalId", item.GetPrimaryKeyValue());
				varPortalPeopleXref.Save();
			}
		}
		public static void SavePortalMap(int varPersonId, System.Web.UI.WebControls.ListItemCollection itemList) 
		{
			QueryCommandCollection coll = new SubSonic.QueryCommandCollection();
			//delete out the existing
			 QueryCommand cmdDel = new QueryCommand("DELETE FROM [PortalPeopleXref] WHERE [PortalPeopleXref].[PersonId] = @PersonId", Person.Schema.Provider.Name);
			cmdDel.AddParameter("@PersonId", varPersonId, DbType.Int32);
			coll.Add(cmdDel);
			DataService.ExecuteTransaction(coll);
			foreach (System.Web.UI.WebControls.ListItem l in itemList) 
			{
				if (l.Selected) 
				{
					PortalPeopleXref varPortalPeopleXref = new PortalPeopleXref();
					varPortalPeopleXref.SetColumnValue("PersonId", varPersonId);
					varPortalPeopleXref.SetColumnValue("PortalId", l.Value);
					varPortalPeopleXref.Save();
				}
			}
		}
		public static void SavePortalMap(int varPersonId , int[] itemList) 
		{
			QueryCommandCollection coll = new SubSonic.QueryCommandCollection();
			//delete out the existing
			 QueryCommand cmdDel = new QueryCommand("DELETE FROM [PortalPeopleXref] WHERE [PortalPeopleXref].[PersonId] = @PersonId", Person.Schema.Provider.Name);
			cmdDel.AddParameter("@PersonId", varPersonId, DbType.Int32);
			coll.Add(cmdDel);
			DataService.ExecuteTransaction(coll);
			foreach (int item in itemList) 
			{
				PortalPeopleXref varPortalPeopleXref = new PortalPeopleXref();
				varPortalPeopleXref.SetColumnValue("PersonId", varPersonId);
				varPortalPeopleXref.SetColumnValue("PortalId", item);
				varPortalPeopleXref.Save();
			}
		}
		
		public static void DeletePortalMap(int varPersonId) 
		{
			QueryCommand cmdDel = new QueryCommand("DELETE FROM [PortalPeopleXref] WHERE [PortalPeopleXref].[PersonId] = @PersonId", Person.Schema.Provider.Name);
			cmdDel.AddParameter("@PersonId", varPersonId, DbType.Int32);
			DataService.ExecuteQuery(cmdDel);
		}
		
		#endregion
		
        
        
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(string varFirstName,string varLastName,string varEmail,int? varCompanyId,int? varRoleId,int? varManagerPersonId,string varUserType)
		{
			Person item = new Person();
			
			item.FirstName = varFirstName;
			
			item.LastName = varLastName;
			
			item.Email = varEmail;
			
			item.CompanyId = varCompanyId;
			
			item.RoleId = varRoleId;
			
			item.ManagerPersonId = varManagerPersonId;
			
			item.UserType = varUserType;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}
		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(int varPersonId,string varFirstName,string varLastName,string varEmail,int? varCompanyId,int? varRoleId,int? varManagerPersonId,string varUserType)
		{
			Person item = new Person();
			
				item.PersonId = varPersonId;
			
				item.FirstName = varFirstName;
			
				item.LastName = varLastName;
			
				item.Email = varEmail;
			
				item.CompanyId = varCompanyId;
			
				item.RoleId = varRoleId;
			
				item.ManagerPersonId = varManagerPersonId;
			
				item.UserType = varUserType;
			
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}
		#endregion
        
        
        
        #region Typed Columns
        
        
        public static TableSchema.TableColumn PersonIdColumn
        {
            get { return Schema.Columns[0]; }
        }
        
        
        
        public static TableSchema.TableColumn FirstNameColumn
        {
            get { return Schema.Columns[1]; }
        }
        
        
        
        public static TableSchema.TableColumn LastNameColumn
        {
            get { return Schema.Columns[2]; }
        }
        
        
        
        public static TableSchema.TableColumn EmailColumn
        {
            get { return Schema.Columns[3]; }
        }
        
        
        
        public static TableSchema.TableColumn CompanyIdColumn
        {
            get { return Schema.Columns[4]; }
        }
        
        
        
        public static TableSchema.TableColumn RoleIdColumn
        {
            get { return Schema.Columns[5]; }
        }
        
        
        
        public static TableSchema.TableColumn ManagerPersonIdColumn
        {
            get { return Schema.Columns[6]; }
        }
        
        
        
        public static TableSchema.TableColumn UserTypeColumn
        {
            get { return Schema.Columns[7]; }
        }
        
        
        
        #endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string PersonId = @"PersonId";
			 public static string FirstName = @"FirstName";
			 public static string LastName = @"LastName";
			 public static string Email = @"Email";
			 public static string CompanyId = @"CompanyId";
			 public static string RoleId = @"RoleId";
			 public static string ManagerPersonId = @"ManagerPersonId";
			 public static string UserType = @"UserType";
						
		}
		#endregion
		
		#region Update PK Collections
		
        public void SetPKValues()
        {
}
        #endregion
    
        #region Deep Save
		
        public void DeepSave()
        {
            Save();
            
}
        #endregion
	}
}
